package com.yang.qqclient.service;

import com.yang.common.Message;
import com.yang.common.MessageType;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @Description: 提供和消息发送的方法
 * @Author: YANG QIANG
 * @Time: 2022-12-21 21:07
 */
public class MessageClientService {

    /**
     * 发送私聊消息
     * @param content
     * @param senderId
     * @param getterId
     */
    public void sendMessage(String content, String senderId, String getterId){
        // 构建message对象
        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setSendTime(new Date().toString());
        message.setMessageType(MessageType.MESSAGE_C0MM_MES);
        // 发送非服务端
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 群发消息
     * @param content1
     * @param senderId
     */
    public void sendMessageToAll(String content1, String senderId) {

        // 构建message对象
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(content1);
        message.setSendTime(new Date().toString());
        message.setMessageType(MessageType.MESSAGE_C0MM_MES_TO_ALL);
        System.out.println(senderId + " @所有人: " + content1);
        // 发送非服务端
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
